package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Model.Approval;

/**
 * Servlet implementation class RoleTable
 */

public class CommitteeApprovalTable extends HttpServlet {
	private static final long serialVersionUID = 1L;
    JspWriter out;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommitteeApprovalTable() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public CommitteeApprovalTable(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		ArrayList<Approval> apptTable = new ArrayList<Approval>();
		apptTable = Database.StaffAppointmentDB.getAllAppointments("4");
		
		for(int i = 0; i < apptTable.size(); i++)
		{	
			String description = "-";
			if(!apptTable.get(i).getDescription().equals("Default"))
				description = apptTable.get(i).getDescription();
			
			out.print("<tr>");
			//check boxes
				out.print("<td>");
				out.print("<input type =\"checkbox\" name=\"check\" value=\""+ apptTable.get(i).getMd5() + "\" />");
				out.print("</td>");
			
				out.print("<td>");
				out.print(apptTable.get(i).getName());
				out.print("</td>");
				
				out.print("<td>");
				out.print(apptTable.get(i).getAppointment());
				out.print("</td>");
				
				out.print("<td>");
				String appointmentID = apptTable.get(i).getAppointmentID(); 
				String setJSFunction = " onClick=\"setPopup('" + appointmentID + "', event)\" ";
				out.print("<a href=\"#\" " + setJSFunction + ">");
				out.print("<img src=\" images/info_icon.png \" width=\"16px\" height=\"16px \">");
				out.print("</a>");
				out.print("</td>");
				
				out.print("<td>");
				out.print(formatdate(apptTable.get(i).getDate()));
				out.print("</td>");
				
//				out.print("<td>");
//				out.print("<a href='staffapp_approve.jsp?id=" + apptTable.get(i).getMd5() + "'>Choose...</a>");
//				out.print("</td>");
			out.print("</tr>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
}
